package com.mfangsoft.zhuangjialong.integration.gift.controller;





import java.text.ParseException;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mfangsoft.zhuangjialong.app.entryactivity.model.BaseEntryActivity;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;

import com.mfangsoft.zhuangjialong.integration.gift.model.BaseGift;
import com.mfangsoft.zhuangjialong.integration.gift.service.GiftService;





@Controller
@RequestMapping("/server")
public class GiftController {
	
	@Autowired
	private GiftService giftService;
	
	/**
	 * 创建礼品
	 * @param 
	 * @return
	 */
	
	@RequestMapping(value="/addGift",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> addActivity(@RequestBody BaseGift baseGift){
		ResponseMessage<String> message = new ResponseMessage<String>();
		/*BaseGift baseGift = new BaseGift();*/
		//baseGift.setName(UserContext.getCurrentUser().getTag());
		//baseActivityInfo.setCreater((String) session.getAttribute("user"));
		/*System.out.println(UserContext.getCurrentUser().getTag());
		System.out.println(UserContext.getCurrentUser().getUserInfoEntity().getName());*/
		try {
			baseGift.setNum(0);
			giftService.addGift(baseGift);
	
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
		
		
	}

	/**
	 * 活动编辑
	 * @param 
	 * @return
	 * @throws 
	 */
	
	@RequestMapping(value="/updateGiftOne",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<BaseGift> updateGiftOne(@RequestBody BaseGift baseGift,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<BaseGift> message = new ResponseMessage<BaseGift>();
		/*BaseActivityInfo baseActivityInfo = new BaseActivityInfo();
		baseActivityInfo.setId(25);*/
		try {
			giftService.updateGiftOne(baseGift);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 礼品查询
	 * @param 
	 * @return
	 * @throws  
	 */
	
	@RequestMapping(value="/selectGiftOne/{id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<BaseGift> selectGiftOne(@PathVariable int id,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<BaseGift> message = new ResponseMessage<BaseGift>();
		try {
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(giftService.selectGiftOne(id));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 礼品列表分页
	 * @param 
	 * @return
	 * @throws  
	 */
	
	@RequestMapping(value="/selectGiftActivityPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Page<Map<String, Object>> selectGiftActivityPage(@RequestBody Page<Map<String, Object>> page) throws ParseException{
		
		
		return giftService.selectGiftActivity(page);
	}
	/**
	 * 礼品列表活动
	 * @param 
	 * @return
	 * @throws  
	 */
	
	@RequestMapping(value="/selectGiftActivity",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<List<BaseGift>> selectGiftActivity() throws ParseException{
		
		
		ResponseMessage<List<BaseGift>> message = new ResponseMessage<List<BaseGift>>();
		try {
			BaseGift baseGift = new BaseGift();
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			
			message.setResult(giftService.selectGift(baseGift));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

	
}
